//
//  AdvertisingListCell.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/5/3.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit
protocol AdvertListDelegate:class {
    func changeOrderStates(index:Int)
    func editOrder(index:Int)
}
class AdvertisingListCell: BaseTableViewCell {
    weak var delegate:AdvertListDelegate?
    var index:Int = 0
    lazy var quoteLabel: UILabel = {
        let label = BaseLabel(style: .oneStyle)
        label.font = UIFont.systemFont(ofSize: 12)
        return label
    }()
    
    lazy var paymentImageView: UIImageView = {
        let imageView = UIImageView()
//        label.font = UIFont.systemFont(ofSize: 10)
//        label.layer.borderColor = kThemeColor.cgColor
//        label.layer.borderWidth = 0.5
//        label.layer.cornerRadius = 7
//        label.textAlignment = NSTextAlignment.center
//        label.textColor = kThemeColor
//        label.text = "支付宝"
        return imageView
    }()
    
    lazy var transactionLabel: UILabel = {
        let label = BaseLabel(style: .thirdStyle)
        label.font = UIFont.systemFont(ofSize: 12)
        label.textColor = KCommonTextColor
        return label
    }()
    
    lazy var timeLabel: UILabel = {
        let label = BaseLabel(style: .thirdStyle)
        label.font = UIFont.systemFont(ofSize: 10)
        label.textColor = KCommonTextColor
        return label
    }()
    
    lazy var undercarriageBtn: UIButton = {
        let btn = UIButton()
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 12)
        btn.addTarget(self, action: #selector(undercarriageBtnAction), for: UIControlEvents.touchUpInside)
        btn.setTitleColor(kThemeColor, for: UIControlState.normal)
        btn.layer.cornerRadius = 14
        btn.layer.borderColor = kThemeColor.cgColor
        btn.layer.borderWidth = 0.5
        return btn
    }()
    
    lazy var editBtn: UIButton = {
        let btn = UIButton()
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 12)
        btn.addTarget(self, action: #selector(editOrderAction), for: UIControlEvents.touchUpInside)
        btn.setTitle(kText("编辑"), for: UIControlState.normal)
        btn.setTitleColor(kThemeColor, for: UIControlState.normal)
        btn.layer.cornerRadius = 14
        btn.layer.borderColor = kThemeColor.cgColor
        btn.layer.borderWidth = 0.5
        return btn
    }()
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override func setUpUI() {
        super.setUpUI()
        self.line.isHidden = true
        self.contentView.addSubview(quoteLabel)
        quoteLabel.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(12)
            make.left.equalToSuperview().offset(16)
            make.width.lessThanOrEqualTo(kSCREEN_WIDTH-145)
        }
        
        self.contentView.addSubview(paymentImageView)
        paymentImageView.snp.makeConstraints { (make) in
            make.left.equalTo(quoteLabel.snp.right).offset(8)
            make.centerY.equalTo(quoteLabel)
        }
        
        self.contentView.addSubview(transactionLabel)
        transactionLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(16)
            make.top.equalToSuperview().offset(40)
            make.width.lessThanOrEqualTo(kSCREEN_WIDTH-86)
        }
        
        self.contentView.addSubview(timeLabel)
        timeLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(16)
            make.top.equalToSuperview().offset(70)
            make.width.lessThanOrEqualTo(kSCREEN_WIDTH-86)
        }
        
        self.contentView.addSubview(undercarriageBtn)
        undercarriageBtn.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-16)
            make.top.equalToSuperview().offset(12)
            make.height.equalTo(28)
            make.width.equalTo(68)
        }
        
        self.contentView.addSubview(editBtn)
        editBtn.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-16)
            make.top.equalToSuperview().offset(55)
            make.height.equalTo(28)
            make.width.equalTo(68)
        }
        
    }
    
    override func setModel(model: BaseModel) {
        let newModel = model as! AdvertInforModel
        quoteLabel.text = String(format: "%@：%@%@",kText("报价"),CYCalculate.cy_intercept(number: newModel.price, digit: 2)!,newModel.currency)
        if newModel.limitLowerMoney > newModel.limitUpperMoney {
            transactionLabel.text = String(format: "%@：--",kText("交易限额"))
        }else {
        transactionLabel.text = String(format: "%@：%@～%@CNY",kText("交易限额"),CYCalculate.cy_intercept(number: newModel.limitLowerMoney, digit: 2)!,CYCalculate.cy_intercept(number: newModel.limitUpperMoney, digit: 2)!)
        }
        timeLabel.text = String(format: "%@：%@", kText("创建时间"),newModel.createdAt.ch_length != 0 ? newModel.createdAt : "")
        if newModel.paymentMethod == 1 {
            paymentImageView.image = UIImage(named: "aliPay")
        }else if newModel.paymentMethod == 2{
            paymentImageView.image = UIImage(named: "unionPay")
        }else {
            paymentImageView.image = UIImage(named: "")
        }
        
    }
    
    @objc func undercarriageBtnAction()  {
        if delegate != nil {
            self.delegate?.changeOrderStates(index: index)
        }
    }
    
    @objc func editOrderAction() {
        if delegate != nil {
            self.delegate?.editOrder(index: index)
        }
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
